﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Angel.Entities;
using Angel.Daos.Manager;
using Angel.Daos.IManager;
using Angel.Web.Models;

namespace Angel.Web.Controllers
{
    public class RoleController : Controller
    {
        private IRoleManager roleManager;
        public RoleController()
            :this(new RoleManager())
        {
        }
        public RoleController(IRoleManager _roleManager)
        {
            roleManager = _roleManager;
        }
        //
        // GET: /Role/
        public ActionResult Index()
        {
            //System.Diagnostics.Debug.WriteLine("# Index Role");
            ViewBag.Message = this.TempData["index"];
            ViewBag.Roles = roleManager.GetAll();
            return View();
        }

        public ActionResult Delete(int? id)
        {
            try
            {
                if (id.Value == 0)
                {
                    return RedirectToAction("NotFound", "General", "Error id");
                }
                var role = roleManager.GetById(id.Value);
                DeleteRoleModels delModel = new DeleteRoleModels();
                delModel.Id = role.ID;
                delModel.Name = role.Name;
                delModel.Description = role.Description;
                return View(delModel);
            }
            catch {
                return RedirectToAction("NotFound", "General", @"Error definition");
            }
            
        }
        [HttpPost]
        public ActionResult Delete(DeleteRoleModels delRole)
        {
            var del=roleManager.GetById(delRole.Id);
            roleManager.Delete(del);
            return RedirectToAction("Index");
        }                
        [HttpPost]
        public ActionResult Add(AddRoleModel addRoleModel)
        {
            //System.Diagnostics.Debug.WriteLine("# Add Role");
            ViewBag.Roles = roleManager.GetAll();
            if(addRoleModel.Name == null)
            {
                //addRoleModel.Name = addRoleModel.Name;
                System.Diagnostics.Debug.WriteLine("#Role name is not valid");
                return View("Index", addRoleModel);
            }
            else
            {
                Role role = new Role();
                role.Name = addRoleModel.Name;
                role.Description = addRoleModel.Description;
                Boolean result = roleManager.Add(role);
                if (result == false)
                {
                    System.Diagnostics.Debug.WriteLine("#Role name already exists");
                    this.TempData["index"] = "A project role with name '"+ addRoleModel.Name +"' already exists.";
                }
                return RedirectToAction("Index");
            }
        }
        //Get/id
        public ActionResult Edit(int? id) 
        {
            try
            {
                if (id == null)
                {
                    return RedirectToAction("NotFound", "General", "Errror id can not be null");
                }
                else
                {
                    var role = roleManager.GetById(id.Value);
                     EditRoleModels edit = new EditRoleModels();
                     edit.Id = role.ID;
                     edit.Name = (role.Name!=null)?role.Name.Trim():"";
                     edit.Description =(role.Description != null) ? role.Description.Trim() : "";
                    return View(edit);
                }
            }
            catch
            {
                return RedirectToAction("NotFound", "General", @"Error definition");
            }
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(EditRoleModels edit)
        {
            try
            {
                var role = roleManager.GetById(edit.Id);
                if (edit.Name == null)
                {
                    ViewBag.nameNull = "The Name field is required.";
                }
                else
                {
                    if (!roleManager.CheckNameExist(edit.Name, edit.Id))
                    {
                        ViewBag.NameErr = "";
                        ViewBag.nameNull = null;
                        UpdateModel(role);
                        roleManager.Save(role);
                        //Go back to the home
                        return RedirectToAction("Index");
                    }
                    else
                        ViewBag.NameErr = "Name \"" + edit.Name + "\" is exist in Project Roles";
                }
            }
            catch (Exception e)
            {
                ViewBag.MessageErr = e.Message;

            }
            return View();
        }
    }
}
